﻿window.onerror = function (message, url, line) {
    if (!url) return;
    var msg = {};

    //记录客户端环境
    msg.ua = window.navigator.userAgent;

    //只记录message里的message属性就好了，
    //错误信息可能会比较晦涩，有些信息完全无用，应酌情过滤
    msg.message = message;
    msg.url = url;
    msg.line = line;
    msg.page = window.location.href;
    msg.number = 0;
    msg.stack = message + "\n" + msg.url + "\n" + msg.page + "\n line:" + line;

    //var s = [];

    //将错误信息转换成字符串
    //for (var key in msg) {
    //    s.push(key + '=' + msg[key]);
    //}
    //s = s.join('&');

    //这里是用增加标签的方法调用日志收集接口，优点是比较简洁。
   // new Image().src = '/ajax-jserror.php?' + encodeURIComponent(s) + '&t=' + Math.random();
    save_error('bas_error', 'onerror', '', 'js', msg)
};

function save_error(PROG_NAME, FUNC_NAME, USER, ERR_STATUS, ex) {
    $.ajax({
        type: "POST",
        url: "/KerryWebSite/Management/Basic/bas_trans_to_server.aspx/SAVE_ERROR",
        data: "{ PROG_NAME :'" + PROG_NAME + "',FUNC_NAME :'" + FUNC_NAME + "',USER :'" + USER + "' ,error_msg :'" + ex.message.replace(/\'/g, "") + "',ERR_STATUS :'" + ERR_STATUS + "',ERR_CODE :'" + ex.number + "' ,ERR_DATA : '" + ex.stack.replace(/\'/g, "") + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        error: function (xmlHttpRequest, error) {
            var m_ErrMessage = xmlHttpRequest.responseText;
            alert(m_ErrMessage);

        },
        success: function (data) {

        }
    });
}